#pragma once
#include "Device_Motor.h"

// FMC4030 返回值定义
enum FMC4030_ReturnCode {
    FMC4030_SUCCESS = 0,        // 执行成功
    FMC4030_CONNECT_FAIL = -1,  // 连接失败
    FMC4030_DATA_FAIL = -4,     // 数据建立失败
    FMC4030_SEND_FAIL = -5,     // 数据发送失败
    FMC4030_RECV_FAIL = -6,     // 数据接收失败
    FMC4030_DATA_ERROR = -7,    // 接收数据错误
    FMC4030_NULL_PTR = -8       // 空指针错误
};

// 轴定义
enum FMC4030_Axis {
    AXIS_X = 0,  // X轴
    AXIS_Y = 1,  // Y轴
    AXIS_Z = 2   // Z轴
};

// 运动模式
enum FMC4030_MotionMode {
    RELATIVE_MOTION = 1,  // 相对运动
    ABSOLUTE_MOTION = 2   // 绝对运动
};

// 停止模式
enum FMC4030_StopMode {
    DECEL_STOP = 1,      // 减速停止
    IMMEDIATE_STOP = 2   // 立即停止
};

// 回零方向
enum FMC4030_HomeDir {
    HOME_POSITIVE = 1,  // 正限位回零
    HOME_NEGATIVE = 2   // 负限位回零
};

/**
 * @brief 简化版FuYu电机控制类
 *
 * 继承Device_Motor基类，实现基础的电机控制功能
 * 移除复杂的插补、IO、485通信等功能，只保留核心运动控制
 */
class Device_Motor_FuYu : public Device_Motor {
    Q_OBJECT

public:
    explicit Device_Motor_FuYu(QObject* parent = nullptr);
    virtual ~Device_Motor_FuYu();

    // 实现Device_Motor的虚函数
    bool Connect() override;
    bool Disconnect() override;
    bool MoveTo(double position, MotionMode mode = MotionMode::Absolute) override;
    double GetPosition() override;
    bool Home(HomeDirection direction = HomeDirection::Negative) override;
    bool Stop(StopMode mode = StopMode::Decelerate) override;
    bool SetSpeed(double speed) override;
    bool SetAcceleration(double acc) override;
    bool SetDeceleration(double dec) override;
    double GetSpeed() override;
    bool IsMoving() override;

    // FuYu特有的配置方法（简化版）
    bool SetAxis(int axis);
    bool SetDeviceId(int id);
    bool SetIPAddress(const QString& ip);
    bool setPort(int port);

private:
    // 设备参数
    int m_deviceId;
    int m_axis;
    QString m_ipAddress;
    int m_port;

    // 运动状态
    double m_currentPosition;
    bool m_isConnected;

    // 辅助方法
    bool CheckResult(int result, const QString& operation);
    MotionMode ConvertToFMC4030Mode(MotionMode mode);
    FMC4030_StopMode ConvertToFMC4030StopMode(StopMode mode);
    FMC4030_HomeDir ConvertToFMC4030HomeDir(HomeDirection direction);
};